package rubik.mutators;

import java.util.ArrayList;

import rubik.ICubeSolution;
import rubik.seqs.Sequence;

public class LastSeqMutator extends AbstractMutator {

	@Override
	protected void mutateSolution(ICubeSolution sol) {
		int r = R.nextInt(3);
		if (sol.getSeqs().size() > 1) {
			switch (r) {
			case 0:
				this.addLast(sol);
				return;
			case 1:
				this.removeLast(sol);
				return;
			case 2:
				this.changeLast(sol);
				return;
			default:
				return;
			}
		} else {
			r = R.nextInt(2);
			switch (r) {
			case 0:
				this.addLast(sol);
				return;
			case 1:
				this.changeLast(sol);
				return;
			default:
				return;
			}
		}

	}

	private void changeLast(ICubeSolution sol) {
		ArrayList<Sequence> seqs = sol.getSeqs();
		seqs.set(seqs.size() - 1, sol.getSeqGenerator().createRandom());
	}

	private void removeLast(ICubeSolution sol) {
		ArrayList<Sequence> seqs = sol.getSeqs();
		seqs.remove(seqs.size() - 1);
	}

	private void addLast(ICubeSolution sol) {
		ArrayList<Sequence> seqs = sol.getSeqs();
		seqs.add(sol.getSeqGenerator().createRandom());
	}

}
